Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Collections.​Generic.​List<​T>
Assembly: System.Collections
Inheritance: object → List
Implemented Interfaces
- IList`1 (Inherits: ICollection<​T>IEnumerable<​T>IEnumerable)
- IReadOnlyList`1 (Inherits: IEnumerable<​T>IEnumerableIReadOnlyCollection<​T>)
- IList (Inherits: ICollectionIEnumerable)
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
Properties
public
int
Capacity
Gets or sets the total number of elements the internal data structure can hold without resizing.
public
int
Count
Gets the number of elements contained in the <see cref="T:System.Collections.Generic.List`1" /> .
public
T
Item
Methods
public
void
Add​(T item)
Adds an object to the end of the <see cref="T:System.Collections.Generic.List`1" /> .
item
The object to be added to the end of the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
public
void
AddRange​(IEnumerable<​T> collection)
Adds the elements of the specified collection to the end of the <see cref="T:System.Collections.Generic.List`1" /> .
collection
The collection whose elements should be added to the end of the <see cref="T:System.Collections.Generic.List`1" /> . The collection itself cannot be <see langword="null" /> , but it can contain elements that are <see langword="null" /> , if type <paramref name="T" /> is a reference type.
public
ObjectModel.​ReadOnlyCollection<​T>
AsReadOnly​()
Returns a read-only <see cref="T:System.Collections.ObjectModel.ReadOnlyCollection`1" /> wrapper for the current collection.
Returns An object that acts as a read-only wrapper around the current <see cref="T:System.Collections.Generic.List`1" /> .
public
int
BinarySearch​(int index,
int count,
T item,
IComparer?<​T> comparer)
Searches a range of elements in the sorted <see cref="T:System.Collections.Generic.List`1" /> for an element using the specified comparer and returns the zero-based index of the element.
Returns The zero-based index of <paramref name="item" /> in the sorted <see cref="T:System.Collections.Generic.List`1" /> , if <paramref name="item" /> is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than <paramref name="item" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.List`1.Count" /> .
index
The zero-based starting index of the range to search.
count
The length of the range to search.
item
The object to locate. The value can be <see langword="null" /> for reference types.
comparer
The <see cref="T:System.Collections.Generic.IComparer`1" /> implementation to use when comparing elements, or <see langword="null" /> to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default" /> .
public
int
BinarySearch​(T item)
Searches the entire sorted <see cref="T:System.Collections.Generic.List`1" /> for an element using the default comparer and returns the zero-based index of the element.
Returns The zero-based index of <paramref name="item" /> in the sorted <see cref="T:System.Collections.Generic.List`1" /> , if <paramref name="item" /> is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than <paramref name="item" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.List`1.Count" /> .
item
The object to locate. The value can be <see langword="null" /> for reference types.
public
int
BinarySearch​(T item,
IComparer?<​T> comparer)
Searches the entire sorted <see cref="T:System.Collections.Generic.List`1" /> for an element using the specified comparer and returns the zero-based index of the element.
Returns The zero-based index of <paramref name="item" /> in the sorted <see cref="T:System.Collections.Generic.List`1" /> , if <paramref name="item" /> is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than <paramref name="item" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.List`1.Count" /> .
item
The object to locate. The value can be <see langword="null" /> for reference types.
comparer
The <see cref="T:System.Collections.Generic.IComparer`1" /> implementation to use when comparing elements.
-or-
<see langword="null" /> to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default" /> .
public
void
Clear​()
Removes all elements from the <see cref="T:System.Collections.Generic.List`1" /> .
public
bool
Contains​(T item)
Determines whether an element is in the <see cref="T:System.Collections.Generic.List`1" /> .
Returns <see langword="true" /> if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.List`1" /> ; otherwise, <see langword="false" /> .
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
public
List<​TOutput>
ConvertAll​(Converter<​T, TOutput> converter)
Converts the elements in the current <see cref="T:System.Collections.Generic.List`1" /> to another type, and returns a list containing the converted elements.
Returns A <see cref="T:System.Collections.Generic.List`1" /> of the target type containing the converted elements from the current <see cref="T:System.Collections.Generic.List`1" /> .
converter
A <see cref="T:System.Converter`2" /> delegate that converts each element from one type to another type.
public
void
CopyTo​(int index,
T[] array,
int arrayIndex,
int count)
Copies a range of elements from the <see cref="T:System.Collections.Generic.List`1" /> to a compatible one-dimensional array, starting at the specified index of the target array.
index
The zero-based index in the source <see cref="T:System.Collections.Generic.List`1" /> at which copying begins.
array
The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.List`1" /> . The <see cref="T:System.Array" /> must have zero-based indexing.
arrayIndex
The zero-based index in <paramref name="array" /> at which copying begins.
count
The number of elements to copy.
public
void
CopyTo​(T[] array)
Copies the entire <see cref="T:System.Collections.Generic.List`1" /> to a compatible one-dimensional array, starting at the beginning of the target array.
array
The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.List`1" /> . The <see cref="T:System.Array" /> must have zero-based indexing.
public
void
CopyTo​(T[] array,
int arrayIndex)
Copies the entire <see cref="T:System.Collections.Generic.List`1" /> to a compatible one-dimensional array, starting at the specified index of the target array.
array
The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.List`1" /> . The <see cref="T:System.Array" /> must have zero-based indexing.
arrayIndex
The zero-based index in <paramref name="array" /> at which copying begins.
public
int
EnsureCapacity​(int capacity)
Ensures that the capacity of this list is at least the specified <paramref name="capacity" /> . If the current capacity is less than <paramref name="capacity" /> , it is increased to at least the specified <paramref name="capacity" /> .
Returns The new capacity of this list.
capacity
The minimum capacity to ensure.
public
bool
Exists​(Predicate<​T> match)
Determines whether the <see cref="T:System.Collections.Generic.List`1" /> contains elements that match the conditions defined by the specified predicate.
Returns <see langword="true" /> if the <see cref="T:System.Collections.Generic.List`1" /> contains one or more elements that match the conditions defined by the specified predicate; otherwise, <see langword="false" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the elements to search for.
public
T
Find​(Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type <paramref name="T" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
List<​T>
FindAll​(Predicate<​T> match)
Retrieves all the elements that match the conditions defined by the specified predicate.
Returns A <see cref="T:System.Collections.Generic.List`1" /> containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty <see cref="T:System.Collections.Generic.List`1" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the elements to search for.
public
int
FindIndex​(int startIndex,
int count,
Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that starts at the specified index and contains the specified number of elements.
Returns The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
startIndex
The zero-based starting index of the search.
count
The number of elements in the section to search.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
int
FindIndex​(int startIndex,
Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from the specified index to the last element.
Returns The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
startIndex
The zero-based starting index of the search.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
int
FindIndex​(Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
T
FindLast​(Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type <paramref name="T" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
int
FindLastIndex​(int startIndex,
int count,
Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that contains the specified number of elements and ends at the specified index.
Returns The zero-based index of the last occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
startIndex
The zero-based starting index of the backward search.
count
The number of elements in the section to search.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
int
FindLastIndex​(int startIndex,
Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from the first element to the specified index.
Returns The zero-based index of the last occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
startIndex
The zero-based starting index of the backward search.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
int
FindLastIndex​(Predicate<​T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The zero-based index of the last occurrence of an element that matches the conditions defined by <paramref name="match" /> , if found; otherwise, -1.
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.
public
void
ForEach​(Action<​T> action)
Performs the specified action on each element of the <see cref="T:System.Collections.Generic.List`1" /> .
action
The <see cref="T:System.Action`1" /> delegate to perform on each element of the <see cref="T:System.Collections.Generic.List`1" /> .
public
Enumerator<​T>
GetEnumerator​()
Returns an enumerator that iterates through the <see cref="T:System.Collections.Generic.List`1" /> .
Returns A <see cref="T:System.Collections.Generic.List`1.Enumerator" /> for the <see cref="T:System.Collections.Generic.List`1" /> .
public
List<​T>
GetRange​(int index,
int count)
Creates a shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.List`1" /> .
Returns A shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.List`1" /> .
index
The zero-based <see cref="T:System.Collections.Generic.List`1" /> index at which the range starts.
count
The number of elements in the range.
public
int
IndexOf​(T item)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The zero-based index of the first occurrence of <paramref name="item" /> within the entire <see cref="T:System.Collections.Generic.List`1" /> , if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
public
int
IndexOf​(T item,
int index)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from the specified index to the last element.
Returns The zero-based index of the first occurrence of <paramref name="item" /> within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from <paramref name="index" /> to the last element, if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
index
The zero-based starting index of the search. 0 (zero) is valid in an empty list.
public
int
IndexOf​(T item,
int index,
int count)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that starts at the specified index and contains the specified number of elements.
Returns The zero-based index of the first occurrence of <paramref name="item" /> within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that starts at <paramref name="index" /> and contains <paramref name="count" /> number of elements, if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
index
The zero-based starting index of the search. 0 (zero) is valid in an empty list.
count
The number of elements in the section to search.
public
void
Insert​(int index,
T item)
Inserts an element into the <see cref="T:System.Collections.Generic.List`1" /> at the specified index.
index
The zero-based index at which <paramref name="item" /> should be inserted.
item
The object to insert. The value can be <see langword="null" /> for reference types.
public
void
InsertRange​(int index,
IEnumerable<​T> collection)
Inserts the elements of a collection into the <see cref="T:System.Collections.Generic.List`1" /> at the specified index.
index
The zero-based index at which the new elements should be inserted.
collection
The collection whose elements should be inserted into the <see cref="T:System.Collections.Generic.List`1" /> . The collection itself cannot be <see langword="null" /> , but it can contain elements that are <see langword="null" /> , if type <paramref name="T" /> is a reference type.
public
int
LastIndexOf​(T item)
Searches for the specified object and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.Generic.List`1" /> .
Returns The zero-based index of the last occurrence of <paramref name="item" /> within the entire the <see cref="T:System.Collections.Generic.List`1" /> , if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
public
int
LastIndexOf​(T item,
int index)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from the first element to the specified index.
Returns The zero-based index of the last occurrence of <paramref name="item" /> within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that extends from the first element to <paramref name="index" /> , if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
index
The zero-based starting index of the backward search.
public
int
LastIndexOf​(T item,
int index,
int count)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that contains the specified number of elements and ends at the specified index.
Returns The zero-based index of the last occurrence of <paramref name="item" /> within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that contains <paramref name="count" /> number of elements and ends at <paramref name="index" /> , if found; otherwise, -1.
item
The object to locate in the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
index
The zero-based starting index of the backward search.
count
The number of elements in the section to search.
public
bool
Remove​(T item)
Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.List`1" /> .
Returns <see langword="true" /> if <paramref name="item" /> is successfully removed; otherwise, <see langword="false" /> . This method also returns <see langword="false" /> if <paramref name="item" /> was not found in the <see cref="T:System.Collections.Generic.List`1" /> .
item
The object to remove from the <see cref="T:System.Collections.Generic.List`1" /> . The value can be <see langword="null" /> for reference types.
public
int
RemoveAll​(Predicate<​T> match)
Removes all the elements that match the conditions defined by the specified predicate.
Returns The number of elements removed from the <see cref="T:System.Collections.Generic.List`1" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the elements to remove.
public
void
RemoveAt​(int index)
Removes the element at the specified index of the <see cref="T:System.Collections.Generic.List`1" /> .
index
The zero-based index of the element to remove.
public
void
RemoveRange​(int index,
int count)
Removes a range of elements from the <see cref="T:System.Collections.Generic.List`1" /> .
index
The zero-based starting index of the range of elements to remove.
count
The number of elements to remove.
public
void
Reverse​()
Reverses the order of the elements in the entire <see cref="T:System.Collections.Generic.List`1" /> .
public
void
Reverse​(int index,
int count)
Reverses the order of the elements in the specified range.
index
The zero-based starting index of the range to reverse.
count
The number of elements in the range to reverse.
public
List<​T>
Slice​(int start,
int length)
Creates a shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.List`1" /> .
Returns A shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.List`1" /> .
start
The zero-based <see cref="T:System.Collections.Generic.List`1" /> index at which the range starts.
length
The length of the range.
public
void
Sort​()
Sorts the elements in the entire <see cref="T:System.Collections.Generic.List`1" /> using the default comparer.
public
void
Sort​(IComparer?<​T> comparer)
Sorts the elements in the entire <see cref="T:System.Collections.Generic.List`1" /> using the specified comparer.
comparer
The <see cref="T:System.Collections.Generic.IComparer`1" /> implementation to use when comparing elements, or <see langword="null" /> to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default" /> .
public
void
Sort​(Comparison<​T> comparison)
Sorts the elements in the entire <see cref="T:System.Collections.Generic.List`1" /> using the specified <see cref="T:System.Comparison`1" /> .
comparison
The <see cref="T:System.Comparison`1" /> to use when comparing elements.
public
void
Sort​(int index,
int count,
IComparer?<​T> comparer)
Sorts the elements in a range of elements in <see cref="T:System.Collections.Generic.List`1" /> using the specified comparer.
index
The zero-based starting index of the range to sort.
count
The length of the range to sort.
comparer
The <see cref="T:System.Collections.Generic.IComparer`1" /> implementation to use when comparing elements, or <see langword="null" /> to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default" /> .
public
T[]
ToArray​()
Copies the elements of the <see cref="T:System.Collections.Generic.List`1" /> to a new array.
Returns An array containing copies of the elements of the <see cref="T:System.Collections.Generic.List`1" /> .
public
void
TrimExcess​()
Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.Generic.List`1" /> , if that number is less than a threshold value.
public
bool
TrueForAll​(Predicate<​T> match)
Determines whether every element in the <see cref="T:System.Collections.Generic.List`1" /> matches the conditions defined by the specified predicate.
Returns <see langword="true" /> if every element in the <see cref="T:System.Collections.Generic.List`1" /> matches the conditions defined by the specified predicate; otherwise, <see langword="false" /> . If the list has no elements, the return value is <see langword="true" /> .
match
The <see cref="T:System.Predicate`1" /> delegate that defines the conditions to check against the elements.
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object